<?php


namespace App\Http\Controllers\Admin\SystemSet\AppVersion;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Services\Tools\FileService;
use Illuminate\Http\Request;

class AppSettingController extends BaseController
{
    /**
     * @api {get} /system-set/app-version/app-setting app设置信息列表列表
     * @apiVersion 0.1.0
     * @apiName 系统设置-app设置信息列表
     * @apiGroup 系统设置
     *
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiParam {number} app_type app类型 1 安卓  2ios
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
    "list": [
    {
    "id(序号)": 2,
    "app_type(app类型 1 安卓  2ios)": 1,
    "name(配置名称)": "气泡语音全球版配置",
    "heartbeat_frequency(心跳频率s/次)": 60,
    "refresh_frequency(刷新频率min/次)": 5,
    "add_time(添加时间)": "2021-06-25 14:25:54",
    "update_time(修改时间)": null
    },
    {
    "id": 1,
    "app_type": 1,
    "name": "气泡星球全球版配置",
    "heartbeat_frequency": 61,
    "refresh_frequency": 5,
    "add_time": "2021-06-25 14:54:04",
    "update_time": "2021-06-25 14:54:04"
    }
    ],
    "meta": {
    "page": 1,
    "limit": "10",
    "next": null,
    "total": 2
    }
    }
    }
     *
     *
     */
    public function list(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'app_type' => 'filled|numeric',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_version_management')->getAppSettingList($request->input());

        return Response::sendList($list);
    }

    /**
     * @api {post} /system-set/app-version/app-setting app全局设置创建
     * @apiVersion 0.1.0
     * @apiName 系统设置-app全局设置创建
     * @apiGroup 系统设置
     *
     * @apiParam {number} app_type                      app类型 1 安卓  2ios 必传
     * @apiParam {string} name                          配置名称 必传
     * @apiParam {number} heartbeat_frequency           心跳频率s/次 必传
     * @apiParam {number} refresh_frequency             刷新频率min/次 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'app_type' => 'filled|in:1,2',
                'name' => 'filled|string',
                'heartbeat_frequency' => 'filled|numeric',
                'refresh_frequency' => 'filled|numeric',
            ],
            [
                'name.required' => '名称必须存在',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $params = $request->only(['app_type', 'name', 'heartbeat_frequency', 'refresh_frequency']);
        $params['add_time'] = date('Y-m-d H:i:s');

        $rows = app('repo_version_management')->addAppSetting($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /system-set/app-version/app-setting app全局设置修改
     * @apiVersion 0.1.0
     * @apiName 系统设置-app全局设置修改
     * @apiGroup 系统设置
     *
     *
     * @apiParam {number} id
     * @apiParam {number} app_type                      app类型 1 安卓  2ios 必传
     * @apiParam {string} name                          配置名称 必传
     * @apiParam {number} heartbeat_frequency           心跳频率s/次 必传
     * @apiParam {number} refresh_frequency             刷新频率min/次 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'app_type' => 'required|in:1,2',
                'heartbeat_frequency' => 'required|numeric',
                'refresh_frequency' => 'required|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $id = $request->input('id');
        $res = app('repo_version_management');

        $params = $request->only(['app_type', 'heartbeat_frequency', 'refresh_frequency']);

        $rows = $res->saveAppSetting(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }
}